package com.my.roco.controller;

import com.my.roco.pojo.Love;
import com.my.roco.pojo.Pet;
import com.my.roco.pojo.User;
import com.my.roco.service.LoveService;
import com.my.roco.service.PetService;
import com.my.roco.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private LoveService loveService;
    @Autowired
    private PetService petService;
    
    @GetMapping("/login")
    public String showLoginPage() {
        return "login";
    }

    @PostMapping("/login")
    public ModelAndView login(@RequestParam("account") String account,
                              @RequestParam("password") String password,
                              HttpSession session) {
        ModelAndView mv = new ModelAndView();
        User user = userService.login(account, password);
        if (user != null) {
            session.setAttribute("user", user);
            session.setAttribute("username", user.getUsername()); 
            mv.setViewName("redirect:/petList");
        } else {
            mv.addObject("error", "用户名或密码错误");
            mv.setViewName("login");
        }
        return mv;
    }

    @GetMapping("/register")
    public String showRegisterPage() {
        return "register";
    }

    @PostMapping("/register")
    public ModelAndView register(@RequestParam("username") String username,
                                 @RequestParam("account") String account,
                                 @RequestParam("password") String password,
                                 @RequestParam("contact") String contact) {
        ModelAndView mv = new ModelAndView();
        User user = new User();
        user.setUsername(username);
        user.setAccount(account);
        user.setPassword(password);
        user.setContact(contact);
        userService.registerUser(user);
        mv.addObject("message", "注册成功，请登录");
        mv.setViewName("login");
        return mv;
    }

    @GetMapping("/guestLogin")
    public String guestLogin(HttpSession session) {
        session.setAttribute("user", null);
        return "redirect:/home";
    }
    
    @GetMapping("/myLoves")
    public ModelAndView showMyLoves(HttpSession session) {
        ModelAndView mv = new ModelAndView();
        String userAccount = ((User) session.getAttribute("user")).getAccount();
        List<Love> loves = loveService.getLovesByUserAccount(userAccount);
        List<Pet> lovedPets = new ArrayList<>();
        for (Love love : loves) {
            if (love != null) {  
                Pet pet = petService.getPetByNumber(love.getPetNumber());
                if (pet != null) { 
                    lovedPets.add(pet);
                }
            }
        }
        mv.addObject("pets", lovedPets);
        mv.setViewName("myLoves");
        return mv;
    }
    
    @GetMapping("/editInfo")
    public String editInfo(HttpSession session, Model model) {
        String account = ((User) session.getAttribute("user")).getAccount();
        User user = userService.getUserByAccount(account);
        model.addAttribute("user", user);
        return "editUserInfo";
    }
    
    @PostMapping("/updateInfo")
    public String updateInfo(User user, HttpSession session, RedirectAttributes redirectAttributes) {
        try {
            userService.updateUser(user);
            User updatedUser = userService.getUserByAccount(user.getAccount());
            session.setAttribute("user", updatedUser);
            redirectAttributes.addFlashAttribute("message", "用户信息更新成功");
            return "redirect:/myInfo";
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("error", "更新失败：" + e.getMessage());
            return "redirect:/editInfo";
        }
    }
}